// packageJ/coffee_machine/energy_machine_Good/energy_machine_Good.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    buttons:[],

    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split(",");
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split("=");
          if(chil_arr[0] == 'machine_id'){
            this.options.machine_id = chil_arr[1]
          }
          if(chil_arr[0] == 'mid'){
            app._setMid(chil_arr[1]);
          }
        }
      }
    }
    this.getData();
    this.bottomButton();
  },
  toBuy(e){
    let { item } = e.currentTarget.dataset;
    let runningTime = item.has_one_plugin_goods.run_time || "";
    if(item.plugin_id == 32){
      if(item.has_one_store_goods){
        wx.navigateTo({
          url: `/packageD/buy/myOrder_v2/myOrder_v2?tag=-2&store_id=${item.has_one_store_goods.store_id}&goodsId=${item.goods_id}&total=1&energy_machine_id=${this.options.machine_id}&runningTime=${runningTime}` ,
        });
      }
    } else {
      wx.navigateTo({
        url: `/packageD/buy/myOrder_v2/myOrder_v2?tag=-2&goodsId=${item.goods_id}&total=1&energy_machine_id=${this.options.machine_id}&runningTime=${runningTime}` ,
      });
    }
  },
  toUrl(e){
    let { url } = e.currentTarget.dataset;
    if(!url) return;
    wx.navigateTo({
      url,
      fail:(err)=>{
        console.log(err)
      }
    })
  },
  bottomButton() {
    let json = {};
    let urlStr = app.getNetAddresss("plugin.energy-machine.frontend.goods.bottomButton");
    json.energy_machine_id = this.options.machine_id;
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            buttons:res.data
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  },
  getData() {
    let json = {};
    let urlStr = app.getNetAddresss("plugin.energy-machine.frontend.goods.searchGoods");
    json["search[energy_machine_id]"] = this.options.machine_id;
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            list: res.data.data
          })
          this.data.isLoadMore = true;
          this.data.total_page = res.data.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let json = {
        page: this.data.page
      };
      let urlStr = app.getNetAddresss("plugin.energy-machine.frontend.goods.searchGoods");
      json["search[energy_machine_id]"] = this.options.machine_id;
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              list: res.data.data
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})